module ApplicationHelper
  include LocalizationSettings

	def current_top_category(category)
		cat = category.ancestors
		cat << category
		cat.flatten!

		ctc = cat & @menu
		return ctc[0]
	end

	def link_to_submenu(current_top_category, current_category)
		html = []
		i = 0
		for c in current_top_category.children
			html << "<li"
			if i == 0 && c.permalink != current_category.permalink
				html << " class='first'><a href='/category/#{c.permalink}'>#{c.name}</a></li>\n"
			elsif i == 0 && c.permalink == current_category.permalink
				html << " class='first'><a class='current' href='/category/#{c.permalink}'>#{c.name}</a></li>\n"
			elsif i != 0 && c.permalink == current_category.permalink
				html << "><a class='current' href='/category/#{c.permalink}'>#{c.name}</a></li>\n"
			else
				html << "><a href='/category/#{c.permalink}'>#{c.name}</a></li>\n"
			end
			i+= 1
		end
		return html
	end

	def current_top_category_class_current(m)
		return if @current_top_category.nil?
		" class='current'" if m.permalink == @current_top_category.permalink
	end
	
	def current_sub_category_class_current(m)
		return if @current_top_category.nil?
		" class='current'" if m.permalink == @current_top_category.permalink
	end
    
  def markdown(text)
    BlueCloth.new(text.gsub(%r{</?notextile>}, '')).to_html
  end

  def filtertext(text)
    RedCloth.new(text.gsub(%r{</?notextile>}, ''), [:hard_breaks]).to_html
  end

  def read_more(text, link, link_text = 'read more', before_link = "&nbsp;&nbsp;")
    return if text.nil? or text.empty?
    markup = text.chop.split("</").last
    html = []
    html << text[0..text.size-(markup.size+4)]
    html << before_link
    html << link_to(l(link_text), link)
    html << "</#{markup}>"
  end

	def breadcrumbs_category(category)
    return if category.nil?
    cat = []
    cat = category.ancestors.collect {|c| c }
    cat.reject! {|c| c.permalink == "root" or c.permalink == "fr" or c.permalink == "de"}
    html = ""
    html << "<div id='breadcrumbs'>"
    html << "#{link_to 'Eurojob', '/' }"
    html << "&nbsp;>&nbsp;"
    if cat.size > 0
    html << cat.collect {|c| link_to l(c.name), category_url(c) }.join('&nbsp;>&nbsp;')
    html << "&nbsp;>&nbsp;"
    end
    html << category.name.capitalize
    html << "</div>"
  end

  def breadcrumbs_article(article)
    return if article.nil?
    category = []
    category = article.category.ancestors.collect {|c| c }
    category << article.category
    category.reject! {|c| c.permalink == "root" or c.permalink == "fr" or c.permalink == "de"}
    html = ""
    html << "<div id='breadcrumbs'>"
    html << "#{link_to 'Eurojob', '/' }"
    html << "&nbsp;>&nbsp;"
    if category.size > 0
      html << category.collect {|c| link_to l(c.name), category_path(c) }.join('&nbsp;>&nbsp;')
      html << "&nbsp;>&nbsp;"
    end
    html << article.title.capitalize
    html << "</div>"
  end


  

  # load indicator
  # options to insert in the link_to_remote:
  # :loading => "Element.show('load-indicator');",
  # :complete => "Element.hide('load-indicator');" 
  def load_indicator(color = 'black')
    html = ""
    html << "<div id=\"load-indicator\" class=\"#{color}-loader\" style=\"display:none\" >"
    html << "</div>"
  end

  def arrow_indicator 
    html = ""
    html << "<img src=\"/images/indicator_arrows.gif\" id=\"arrow-indicator\" alt=\"arrow indicator\" style=\"display:none;\" />"
  end

  def my_pagination_links(paginator, collection, options = {} )
    unless paginator.page_count < 2
      collection_name = options[:collection_name] || l('Results')
      per_page = options[:per_page] || 10
      params = options[:params] || ""
      current_page = paginator.current_page
      html = ""
    
      html << "<div id='pagination'>"
      html << "<div id='page'>"

      html << l("Pages") + " "
    
      for i in 1..paginator.last.number
        html << (link_to_and_highlight_if_current i, paginator, { :params => params.merge(:page => i)})
        html << "&nbsp;"
      end

      html << "</div>"
      html << "<div id='previous-and-next'>"
      # Previous
      if paginator.current.previous
        html << (link_to "<<&nbsp;" + l('previous'), { :params => params.merge( 
        :page => paginator.current.previous.number )}) 
        html << " "
      end

      # Next
      if paginator.current.next
        html << " "
        html << (link_to l("next") + "&nbsp;>>", { :params => params.merge(
        :page => paginator.current.next.number )}) 
      end
  
      html << "</div>"
      html << "</div>"
      html << "<div class='clear'>&nbsp;</div>"
      return html
    end
  end

  def link_to_and_highlight_if_current(name, paginator, options ={}, html_options={})
    if name.to_s == params[:page] or (params[:page].nil? and name == 1)
      link_to name.to_s, options, html_options.merge( :class => "strong" )
    else
      link_to name.to_s, options, html_options
    end
  end


end
